using System.Web.UI.WebControls;
using System.Web.UI;
using Telerik.Web.UI;
using System;

namespace CarbonExchange.WebControls
{
    public abstract class BaseRadGridColumn : GridBoundColumn
    {
        public BaseRadGridColumn()
        {
            RequiredDisplay = ValidatorDisplay.Dynamic;
        }

        public bool Required { get; set; }
        public string RequiredMessage { get; set; }
        public ValidatorDisplay RequiredDisplay { get; set; }
        public string RequiredValidationGroup { get; set; }
        public string HelpText { get; set; }

        protected override object SaveViewState()
        {
            ViewState["DataFormatString"] = DataFormatString;
            ViewState["HelpText"] = HelpText;
            return base.SaveViewState();
        }


        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            DataFormatString = ViewState["DataFormatString"] as string;
            HelpText = ViewState["HelpText"] as string;
        }

        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem)
        {
            if (inItem is GridHeaderItem || inItem is GridFooterItem)
            {
                base.InitializeCell(cell, columnIndex, inItem);
            }
            else if (!this.ReadOnly && inItem.IsInEditMode)
            {
                InitalizeEditCell(cell, columnIndex, inItem);
                InitalizeValidators(cell, columnIndex, inItem);
            }
            else if (inItem is GridDataItem)
            {
                InitaliseDisplayCell(cell, columnIndex, inItem);
            }
        }

        protected void InitalizeValidators(TableCell cell, int columnIndex, GridItem inItem)
        {
            if (Required && GetControlIdToValidate(inItem) != null)
            {
                RequiredFieldValidator val = new RequiredFieldValidator();
                cell.Controls.Add(val);
                val.ControlToValidate = GetControlIdToValidate(inItem);
                val.Display = RequiredDisplay;
                if (!string.IsNullOrEmpty(RequiredMessage))
                {
                    val.ErrorMessage = RequiredMessage;
                }
                else
                {
                    val.ErrorMessage = "* Required";
                }
                if (!string.IsNullOrEmpty(RequiredValidationGroup))
                {
                    val.ValidationGroup = RequiredValidationGroup;
                }
            }
        }



        protected abstract string GetControlIdToValidate(GridItem item);

        protected abstract void InitalizeEditCell(TableCell cell, int columnIndex, GridItem item);

        protected virtual void InitaliseDisplayCell(TableCell cell, int columnIndex, GridItem item)
        {
            cell.DataBinding += new System.EventHandler(cell_DataBinding);
        }

        void cell_DataBinding(object sender, System.EventArgs e)
        {
            TableCell cell = sender as TableCell;
            if(!string.IsNullOrEmpty(DataField))
            {
                object val = GetDataValue(cell.NamingContainer);
                if (val != null && !string.IsNullOrEmpty(DataFormatString))
                {
                    cell.Text = string.Format(DataFormatString, val.ToString());
                }
                else if (val != null)
                {
                    cell.Text = val.ToString();
                }
            }
            DisplayCellDataBound(cell);
        }

        protected object GetDataValue(Control control)
        {
            object tmp = GetDataItem(control);
            if (tmp != null && !(tmp is GridInsertionObject))
            {
                return DataBinder.Eval(tmp, DataField);
            }
            else
            {
                return null;
            }
        }

        protected object GetDataItem(Control control)
        {
            if (control is GridEditableItem)
            {
                return ((GridEditableItem)control).DataItem;
            }
            else if (control.NamingContainer != null)
            {
                return GetDataItem(control.NamingContainer);
            }
            else
            {
                return null;
            }
        }

        protected virtual void DisplayCellDataBound(TableCell cell)
        {
        }
    }
}
